//
//  JwScreen.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-26.
//

#import "JwScreen.h"

@implementation JwScreen

+ (CGRect)bounds {
    return [[UIScreen mainScreen] bounds];
}

+ (CGSize)size {
    return [[UIScreen mainScreen] bounds].size;
}

+ (CGFloat)width {
    return [[UIScreen mainScreen] bounds].size.width;
}

+ (CGFloat)height {
    return [[UIScreen mainScreen] bounds].size.height;
}

/// 判断刘海屏
+ (BOOL)isScreenBangs {
    return [self bottomBarHeight] > 0;
}

/// 电池栏高度
+ (CGFloat)batBarHeight {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 13.0, *)) {
        UIWindowScene *scene = window.windowScene;
        if (scene && scene.statusBarManager) {
            return scene.statusBarManager.statusBarFrame.size.height;
        }
    }
    return [[UIApplication sharedApplication] statusBarFrame].size.height;
}

/// 导航栏高度
+ (CGFloat)navBarHeight {
    return 44.0;
}

+ (CGFloat)navBatBarHeight {
    return [self navBarHeight] + [self batBarHeight];
}

/// 下巴高度
+ (CGFloat)bottomBarHeight {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets safeAreaInsets = window.safeAreaInsets;
        return safeAreaInsets.bottom;
    }
    return 0;
}

/// tab高度
+ (CGFloat)tabBarHeight {
    return 49.0;
}

+ (CGFloat)tabBottomHeight {
    return [self tabBarHeight] + [self bottomBarHeight];
}

/// 基础尺寸
+ (CGSize)baseSize {
    return CGSizeMake(357.0, 667.0);
}

/// 基础宽度比例
+ (CGFloat)scaleWidth {
    return [self width]/[self baseSize].width;
}

/// keyWindow
+ (UIWindow *)keyWindow {
    UIWindow *keyWindow = nil;
    if (@available(iOS 13.0, *)) {
        NSSet<UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
        for (UIScene *scene in connectedScenes) {
            if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
                UIWindowScene *windowScene = (UIWindowScene *)scene;
                for (UIWindow *window in windowScene.windows) {
                    if (window.isKeyWindow) {
                        keyWindow = window;
                    }
                }
            }
        }
    } else {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    return keyWindow;
}

/// normalWindow
+ (UIWindow *)normalWindow {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [UIApplication sharedApplication].windows;
        for (UIWindow *target in windows) {
            if (target.windowLevel == UIWindowLevelNormal) {
                window = target;
                break;
            }
        }
    }
    return window;
}

/// window
+ (UIWindow *)window {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    if (!window) {
        window = [self keyWindow];
    }
    if (!window) {
        window = [self normalWindow];
    }
    return window;
}

/// 最前导航控制器
+ (UINavigationController *)frontNC {
    UIViewController *vc = [self window].rootViewController;
    return [self frontNCFromVC:vc];
}

/// 最前导航控制器
+ (UINavigationController *)frontNCFromVC:(UIViewController *)vc {
    UIViewController *frontVC = [self frontVCFromVC:vc];
    return frontVC.navigationController;
}

/// 最前控制器
+ (UIViewController *)frontVC {
    UIViewController *vc = [self window].rootViewController;
    UIViewController *fvc = [self frontVCFromVC:vc];
    return fvc;
}

/// 最前控制器
+ (UIViewController *)frontVCFromVC:(UIViewController *)vc {
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UIViewController *sc = ((UITabBarController *)vc).selectedViewController;
        return [self frontVCFromVC:sc];
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UIViewController *pc = ((UINavigationController *)vc).presentedViewController;
        if (pc) {
            return [self frontVCFromVC:pc];
        }
        UIViewController *tc = ((UINavigationController *)vc).topViewController;
        return [self frontVCFromVC:tc];
    } else if ([vc isKindOfClass:[UIViewController class]]) {
        UIViewController *pc = vc.presentedViewController;
        if (pc) {
            return [self frontVCFromVC:pc];
        }
        return vc;
    } else {
        return nil;
    }
}

@end
